﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Spelprojekt
{
    class Enemy
    {
        Vector2 position;
        Texture2D EnemyShip;

        float speed = 150.0f;
        float deltaX = 0.0f;
        float xLength = 0.0f;
        float xStart = 0.0f;
        bool firingActive = true;
        bool firing = false;
        float fireSpeed = 2.0f;
        float totalTime = 0.0f;
        float radius = 26.0f;

        public bool FiringActive
        {
            set { firingActive = value; }
        }

        public bool Firing
        {
            set { firing = value; }
            get { return firing; }
        }

        public Enemy(Texture2D EnemyShip, Vector2 startPosition, float speed)
        {
            this.EnemyShip = EnemyShip;
            position = startPosition;
            this.speed = speed;
        }

        public void SetAcrossMovement(float deltaX, float xLength)
        {
            this.deltaX = deltaX;
            this.xLength = xLength;
            xStart = position.X;
        }

        public Vector2 Position
        {
            get { return position; }
        }

        public float Radius
        {
            get { return radius; }
        }

        public int CollisionBall(List<Fireball> fireballList)
        {
            for (int i = 0; i < fireballList.Count; i++)
            {
                if ((fireballList[i].Position - position).Length() < radius)
                    return i;
            }
            return -1;
        }

        public void Draw(SpriteBatch batch)
        {
            batch.Draw(EnemyShip, position, null, Color.White, 0.0f, new Vector2(
            14.0f, 13.0f), 1.0f, SpriteEffects.FlipVertically, 0.0f);
        }

        public void Update(GameTime gameTime)
        {
            float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
            position.X += deltaX * elapsed;
            if (Position.X < xStart - xLength || Position.X > xStart + xLength)
                deltaX *= -1.0f;
            position.Y += speed * elapsed;
            if (firingActive)
            {
                totalTime += elapsed;
                if (totalTime > fireSpeed)
                {
                    totalTime = 0.0f;
                    firing = true;
                }
            }
        }

    }//Class End
}
